/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Red5UploadPanel.java
 *
 * Created on Mar 26, 2010, 12:45:21 PM
 */

package com.googlecode.mindlistening.gui;

import com.googlecode.mindlistening.PropertiesStorage;
import com.googlecode.mindlistening.upload.Red5MediaUploader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ryan
 */
public class Red5UploadPanel extends javax.swing.JPanel implements UploadPanel {


    private static String STORAGE_NAME = "Red5UploadPanel." + Red5MediaUploader.POST_URL;

    /** Creates new form Red5UploadPanel */
    public Red5UploadPanel() {
        initComponents();
    }


    PropertiesStorage propStorage;
    public Red5UploadPanel(PropertiesStorage propStorage) {
        this();
        this.propStorage = propStorage;
        String storedUrl = propStorage.loadProperty(STORAGE_NAME);
        if (storedUrl != null) {
            red5URL.setText(storedUrl);
        } else {
            red5URL.setText(null);
        }
    }


    public void onGreenbridgeServerChange(String serverUrl) {
        if (serverUrl != null) {
            try {
                URL url = new URL(serverUrl);
                String red5server = url.getProtocol() + "://" + url.getHost() + ":8080/red5plugin/upload";
                red5URL.setText(red5server);
            } catch (MalformedURLException ex) {
                Logger.getLogger(Red5UploadPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        red5URL = new javax.swing.JTextField();

        jLabel1.setText("Red5 Upload URL");

        red5URL.setText("http://localhost:8080/red5plugin/upload");
        red5URL.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                red5URLActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(red5URL, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(red5URL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void red5URLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_red5URLActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_red5URLActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField red5URL;
    // End of variables declaration//GEN-END:variables



    @Override
    public Map retrieveHostingProperties() {

        if ( propStorage != null) {
             propStorage.storeProperty(STORAGE_NAME, red5URL.getText());
        }

        Map props = new HashMap();
        props.put(Red5MediaUploader.POST_URL, red5URL.getText());
        return props;
    }

}
